﻿using System.Configuration;
using System.Reflection;
using Kangaroo.Configuration;
using Kangaroo.IDAL;

namespace Kangaroo.DAL
{
    public class Data
    {
        private static string _path = ConfigurationManager.AppSettings["DAL"];
        private static ISignal _signal;
        private static IAlarm _alarm;
        private static IGeneral _general;

        private static readonly IDataFactory _dataFactory;

        static Data()
        {
            _path = ConfigurationManager.AppSettings[Config.Instance.Data.DatabaseType + "DAL"];
            _dataFactory = (IDataFactory)CreateInstance(_path + ".DataFactory");
            _dataFactory.Init(Config.Instance.Data.ConnectionString);
        }

        public static ISignal Signal
        {
            get
            {
                return _signal ?? (_signal = _dataFactory.CreateSignal());
            }
        }

        public static IAlarm Alarm
        {
            get
            {
                return _alarm ?? (_alarm = _dataFactory.CreateAlarm());
            }
        }

        public static IGeneral General
        {
            get
            {
                return _general ?? (_general = _dataFactory.CreateGeneral());
            }
        }

        private static object CreateInstance(string className)
        {
            return Assembly.Load(_path).CreateInstance(className);
        }
    }
}
